package top.joylee.example.hello.service;


import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.joylee.example.grpc.hello.HelloRequest;
import top.joylee.example.grpc.hello.HelloResponse;
import top.joylee.example.grpc.GrpcStub;

@Slf4j
@Service
public class HelloRpcService {

    @Autowired
    private GrpcStub grpcStub;

    public String hello(String username) {
        //第一步:设置请求体
        HelloRequest request = HelloRequest
                .newBuilder()
                .setName(username)
                .build();

        //定义response
        HelloResponse response;
        try {
            response = grpcStub.getBlockingStub().sayHello(request);
        } catch (StatusRuntimeException e) {
            log.error("gRPC failed,{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        return response.getMessage();
    }


}
